package ca.ets.osgi.planeservice;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Enumeration;

import ca.ets.osgi.planeinterface.IPlaneReservation;


public class PlaneService implements IPlaneReservation<Object>{
	//public ListAirport lap;
	private Calendar calen = Calendar.getInstance();
	
	public PlaneService(){}
	
	@Override
	public Object getTicket(String cityDeparture, String cityArrival,
			String dateDeparture, String dateReturn) {
		boolean found = false;
		System.err.println("[Attempting to make a flight reservation with these input:]");
		System.err.println("[From city: " + cityDeparture + ", From date: " + dateDeparture + ",To city: " +cityArrival + ", Return date: " + dateReturn + "]");
		Object obj = null;
		PlaneTicket ticket = null;
		FlightReader fReader = new FlightReader();
		fReader.openList();
		ListAirport la = ListAirport.getInstance();
		Enumeration<Airport> aEnume = la.getaTable().elements();
		while(aEnume.hasMoreElements()){
			
			//fetching an airport
			Airport aport = aEnume.nextElement();
			
			//if it's the right airport of departure
			if(aport.getAdresse().getVille().equalsIgnoreCase(cityDeparture)){
				Enumeration<FlightAbs> iEnume = aport.getfTable().elements(); 
				while(iEnume.hasMoreElements()){
					FlightInfo flight = (FlightInfo) iEnume.nextElement();
					if(flight.getCityArrival().equalsIgnoreCase(cityArrival)){
						if(flight.isRightFlight(cityDeparture, cityArrival, dateDeparture, dateReturn) ==true){
							//ticket = aport.getTicket(cityDeparture, cityArrival, dateDeparture, dateReturn);
							ticket = aport.getTicket(flight.getFlightID());
							if(ticket !=null){
								
								System.out.println("Your flight reservation is complete");
								System.out.println("On: " + calen.getTime());
								System.out.println("Ticket info: " +  ticket.toString());
								found =true;
								break;
								
							}						
							
						}
						
					}
					
					
				}if(found ==true){
					break;
				}
				System.out.println("No flight matches your initial criterion");
				System.out.println("The system will attempt to find new flights");
				FlightInfo flight = alternativeFlight(aport.getNearFlight(cityDeparture, cityArrival, dateDeparture, dateReturn));
				if(flight.isAvailability() == true){
					ticket = aport.getTicket(flight.getFlightID());
					System.out.println("Your flight reservation is complete");
					System.out.println("On: " + calen.getTime());
					System.out.println(ticket.toString());
					break;
				}
				else {
					System.out.println("No Flights at all matches your criterion");
					break;
				}
			}			
		}
		if(ticket != null){
			obj = convertTicketToObjectList(ticket);
		}
		
		return obj;
	}
	
	/**
	 * Retrieve the alternative flight if available
	 * @param fabs
	 * @return
	 */
	private FlightInfo alternativeFlight(ArrayList<FlightAbs> fabs){
		FlightInfo fInfo = null;
		if(fabs.size()!=0){
			BufferedReader inBR = new BufferedReader(new InputStreamReader(System.in));
			String str;
			while(true){
				try {
					System.out.println("Here'S your choice");
					for(int i = 0; i < fabs.size();i++){
						System.out.println("["+i+"]" + fabs.get(i).toString());
					}
					System.out.println("Which one suits you the best");
					str = inBR.readLine();
					fInfo = (FlightInfo) fabs.get(Integer.parseInt(str));
					//inBR.close();
					break;
							
				}catch(IndexOutOfBoundsException i){
					System.out.println("WRONG CHOICE!!!!");
				}catch (Exception e) {
					e.printStackTrace();
				}
			}
			
		}
		
		return fInfo;
	}
	
	/**
	 * Convert a plane ticket to an object list
	 * @param t
	 * @return
	 */
	private ArrayList<Object> convertTicketToObjectList(PlaneTicket t){
		ArrayList<Object> list = new ArrayList<Object>();
		if(t !=null){
			list.add(t.getAirportID());
			list.add(t.getPhone());
			list.add(t.getFlightID());
			list.add(t.getAirportArrival());
			list.add(t.getAirportDeparture());
			list.add(t.getCityArrival());
			list.add(t.getCityDeparture());
			list.add(t.getDateDeparture());
			list.add(t.getDateReturn());
			list.add(t.getAdresse().getAdresseNum());
			list.add(t.getAdresse().getRue());
			list.add(t.getAdresse().getVille());
			list.add(t.getAdresse().getProvince());
			list.add(t.getAdresse().getCodePostal());
		}
		
		return list;
	}

}
